﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Cart_Goods_BuyController : UserController
    {
        [Authorize]
        [HttpGet]
        public IActionResult Get(string jsondata)//[{article_id=1,goods_id=1,quantity=1}]
        {
            var user = GetUserInfo();
            if (user == null)
            {
                return Ok(new { status = 0, msg = "请先登录！" });
            }
            List<App_cart_keys> ls = (List<App_cart_keys>)JsonHelper.JSONToObject<List<App_cart_keys>>(jsondata);
            for (int i = 0; i < ls.Count; i++)
            {
                ls[i].user_id = user.id;
            }
            string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_" + user.id + "_";
            using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379"))
            {
                IDatabase db = redis.GetDatabase();
                string json = JsonConvert.SerializeObject(ls);
                db.StringSet(redisName + "buy", json, new TimeSpan(7200 * TimeSpan.TicksPerSecond));
                return Ok(new { status = 1, msg = "商品已成功加入购物清单！" });
            }
        }
    }
}